package com.ckm.exception.handler;

import com.ckm.entity.ResponseResult;
import com.ckm.exception.BusinessException;
import io.lettuce.core.RedisCommandExecutionException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获自定义异常，返回规定结果
     * @param e 异常
     * @return 自定义返回结果
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult businessExceptionHandler(BusinessException e){
        log.error(e.getMsg());
        return new ResponseResult(e.getCode(),e.getMsg(),null);
    }

    /**
     * 捕获约束违规异常，返回规定结果
     * @param e 异常
     * @return 自定义返回结果
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseResult constraintViolationExceptionHandler(ConstraintViolationException e){
        log.error(e.getMessage());
        return new ResponseResult(250,e.getMessage(),null);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseResult illegalArgumentExceptionHandler(IllegalArgumentException e){
        log.error(e.getMessage());
        return new ResponseResult(250,e.getMessage(),null);
    }

    /**
     * redis异常处理
     * @param e 异常
     * @return 自定义返回结果
     */
    @ExceptionHandler(RedisCommandExecutionException.class)
    public ResponseResult redisCommandExecutionExceptionHandler(RedisCommandExecutionException e){
        log.error(e.getMessage());
        return new ResponseResult(250,e.getMessage(),null);
    }

    /*
//    @ExceptionHandler(Exception.class)
//    public ResponseResult exceptionHandler(Exception e){
//        log.error(e.getMessage());
//        return new ResponseResult(500,e.getMessage(),null);
//    }
     */

}
